//index.js
//获取应用实例
var cfg = require('../../utils/cfg');
var util = require('../../utils/util');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
var common = require('../../utils/common');
var sendRequest = require('../../utils/requestUtils');

Page({
  data: {
    xx : '',
    remark: '',
    commName: '',
    price: '',
    pictureUrl: '',
    applyButtonHidden: true,
    tempFilePaths: [],
    kouweiTags : [],
    guigeTags : [],
    manjianTags : [],
    canhefeiTag : {},
    selSkuType : '-1',
    currentTag: { 'skuType': '', 'skuName' : '', 'price' : ''},
    skuTypes : [
      {
        'skuType' : 1,
        'skuName': '口味'
      },
      {
        'skuType': 2,
        'skuName': '规格'
      },
      {
        'skuType': 4,
        'skuName': '餐盒费'
      }
    ]
  },
  onLoad: function () {
    let that = this;
  },

  addTag : function() {
    let that = this;
    let currentTag = that.data.currentTag;
    if (currentTag.skuType == '') {
      return;
    }

    if (currentTag.skuType == 1) {
      that.data.kouweiTags.push(currentTag);
    }

    if (currentTag.skuType == 2) {
      that.data.guigeTags.push(currentTag);
    }

    if (currentTag.skuType == 3) {
      that.data.manjianTags.push(currentTag);
    }

    if (currentTag.skuType == 4) {
      that.data.canhefeiTag = currentTag;
    }

    that.setData({
      kouweiTags: that.data.kouweiTags,
      guigeTags: that.data.guigeTags,
      manjianTags: that.data.manjianTags,
      canhefeiTag: that.data.canhefeiTag,
      xx : ''
    });

    that.data.currentTag = { 'skuType': '', 'skuName' : '', 'price' : '' };
  },

  canhefeiChange: function (event) {
    let that = this;
    let price = event.detail.detail.value;
    that.data.currentTag = { 'skuType': 4, 'skuName': '餐盒费', 'price': price };
  },

  guigePriceChange: function (event) {
    let that = this;
    let price = event.detail.detail.value;
    that.data.currentTag.skuType = 2;
    that.data.currentTag.price = price;
  },

  guigeChange: function (event) {
    let that = this;
    let skuName = event.detail.detail.value;
    that.data.currentTag.skuType = 2;
    that.data.currentTag.skuName = skuName;
  },

  kouweiChange : function(event) {
    let that = this;
    let skuName = event.detail.detail.value;
    that.data.currentTag = { 'skuType': 1, 'skuName': skuName, 'price' : '' };
  },
  //下拉框选择事件
  changeSelect: function (result) {
    let that = this;
    console.log(result.detail);
    that.setData({
      selSkuType: result.detail.id
    });
  },

 

  //添加菜品
  shenqing: function () {
    let that = this;
    if (that.data.commName == '' || that.data.commName == undefined) {
      util.alert('请输入菜品名称');
      return;
    }

    if (that.data.price == '' || that.data.price == undefined) {
      util.alert('请输入价格');
      return;
    }

    if (that.data.tempFilePaths.length == 0) {
      util.alert('请选择图片');
      return;
    }

    let skuJsonStr = util.processSkuInfo(that.data);

    wx.uploadFile({
      url: cfg.host + cfg.djurl.addCaiPin,
      filePath: that.data.tempFilePaths[0],
      name: 'file',
      formData: {
        'skuInfo': skuJsonStr,
        'commName': that.data.commName,
        'price': that.data.price,
        'remark': that.data.remark,
        'openId': 'xxx'
      },
      success(res) {
        let result = JSON.parse(res.data);
        if (result.code !== 1000) {
          util.alert(result.message);
          return;
        }

        console.log('添加成功');
        wx.navigateBack({
          delta: 1
        })

      }
    })

  },

  choseImg: function () {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        that.data.tempFilePaths = res.tempFilePaths;
        console.log('>>>res.tempFilePaths=' + res.tempFilePaths[0]);
        that.setData({
          pictureUrl: res.tempFilePaths[0],
          applyButtonHidden: false
        });
      }
    })
  },

  commNameChange: function (event) {
    this.data.commName = event.detail.detail.value;
  },

  remarkChange: function (event) {
    this.data.remark = event.detail.detail.value;
  },

  priceChange: function (event) {
    // let pages = getCurrentPages();
    // let currentPage = pages[pages.length - 1];
    this.data.price = event.detail.detail.value;
  }

})
